#!/usr/bin/env python
import sys
from HollyWidgets.TreeViewBased.TreeView.Events import NodeEventArgs

class NodeCollection():

	def __init__(self):
		#dictionarul de event-uri
		self.Events = { "NodeAdded"   : None, "NodeRemoved" : None, "NodeUpdated" : None }
		self.List   = []
		
	def connect( self, event_name, function ):
		if self.Events.keys().index( event_name ) != -1:
			self.Events[ event_name ] = function
		else:
			print "event '" + event_name + "' does not exist"

	#HTreeNode[] nodes
	def AddRange( self, nodes ):
		for node in nodes: self.Add( node )
                
	def Add ( self, node ):
		self.List.append( node )
		if self.Events["NodeAdded"] != None:
			self.Events["NodeAdded"]( self, NodeEventArgs.NodeEventArgs( node ) )
                
	def Get( self, index ):
		return self.List[index]
		
	def Set( self, index ):
		oldnode = self.List[index]
		self.List[index] = value
		if self.Events["NodeUpdated"] != None:
			self.Events["NodeUpdated"]( self, NodeEventArgs.NodeUpdateEventArgs( oldnode, value ) )

                
	def Sort( self, comparer = None ):
		if comparer == None:
			self.List.Sort()
		else:
			#TODO:
			print "de implementat o sortare proprie"
			
	def RemoveAt( self, index ):
		self.Remove( self.List[index] ) 
		
	def Clear( self ):
		print "count =" + str( self.Count() )
		for node in self.List:
			print node.get_text()
			self.Remove( node )

	def Remove( self, node ):
		if self.Events["NodeRemoved"] != None: 
			self.Events["NodeRemoved"]( self, NodeEventArgs.NodeEventArgs( node ) )
		del self.List[ self.IndexOf( node ) ]
                
	def Contains (self, node):
		return  ( self.List.count(node) == 1 )
                
	def IndexOf( self, node):
		return self.List.index(node)
		
	def Count( self ):
		return len( self.List )
		
	def __str__( self ):
		ret =  "{ "
		for nod in self.List:
			ret = ret + "['" + nod.Text + "']"
		ret = ret + "}"
		return ret
